package it.valeandre.omino;

import it.valeandre.omino.net.NetworkService;
import it.valeandre.omino.net.NetworkServiceListener;
import it.valeandre.omino.utils.ResBundle;
import it.valeandre.omino.utils.Utils;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings.Secure;
import android.util.Log;

public class GCMRegistrationReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(final Context context, Intent intent) {
		String action = intent.getAction();
		Utils.log("@@@@@@@@@@@@@@@@@@@@@  ON RECEIVE" + intent);
		if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
			if(intent.getStringExtra("error") != null) {
				Intent wakeUpActivityIntent = new Intent();
				wakeUpActivityIntent.setAction(ResBundle.KEY_ACTION_MANAGE_REGISTERED);
				wakeUpActivityIntent.putExtra(ResBundle.KEY_INTENT_TOKEN, "error");
				context.sendBroadcast(wakeUpActivityIntent);
			} else {
				Utils.log("i", "C2DMRegistrationReceiver token received: " + intent.getStringExtra("registration_id"));
				final String registrationId = intent.getStringExtra("registration_id");
				String savedToken = Utils.readRegisteredToken(context);
				if((savedToken != null && !savedToken.equals(registrationId))||(savedToken==null)) {
					Utils.log("w", "@@@@@@@@@@@@@@@@@@@@@@ token CHANGED!");
					String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
					
					NetworkService.getInstance().updateToken(deviceId, registrationId, new NetworkServiceListener() {

						@Override
						public void tokenUpdatePerformed(String token) {
							Utils.log("d", "Updating token performed");
							Utils.saveToken(context, registrationId);
//							Intent wakeUpActivityIntent = new Intent();
//							wakeUpActivityIntent.setAction(ResBundle.KEY_ACTION_MANAGE_REGISTERED);
//							wakeUpActivityIntent.putExtra(ResBundle.KEY_INTENT_TOKEN, registrationId);
//							context.sendBroadcast(wakeUpActivityIntent);
							notifyRegistrationPerformed(context, registrationId);
							
						}

						@Override
						public void tokenUpdateFailed(int statusCode) {
							Utils.log("e", "Updating token failed");
						}

						@Override
						public void tokenUpdateFailed(Exception e) {
							Utils.log("e", "Updating token failed");
							e.printStackTrace();
						}

					});
				} else {
					Utils.log("w", "@@@@@@@@@@@@@@@@@@@@@@ token UNCHANGED!");
//					Intent wakeUpActivityIntent = new Intent();
//					wakeUpActivityIntent.setAction(ResBundle.KEY_ACTION_MANAGE_REGISTERED);
//					wakeUpActivityIntent.putExtra(ResBundle.KEY_INTENT_TOKEN, registrationId);
//					context.sendBroadcast(wakeUpActivityIntent);
					notifyRegistrationPerformed(context, registrationId);
				}
				
			}
		}
	}
	
	private void notifyRegistrationPerformed(Context context, String registrationId) {
		Intent wakeUpActivityIntent = new Intent();
		wakeUpActivityIntent.setAction(ResBundle.KEY_ACTION_MANAGE_REGISTERED);
		wakeUpActivityIntent.putExtra(ResBundle.KEY_INTENT_TOKEN, registrationId);
		context.sendBroadcast(wakeUpActivityIntent);
	}
	
}